/* eslint valid-jsdoc: "off" */

'use strict';

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1664441401327_8862';

  // add your middleware config here
  config.middleware = [];

  // 配置数据库
  config.mongoose = {
    url: 'mongodb://mongo/test',
    options: {},
  };
  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  config.security = {
    csrf: {
      enable: false,
    },
    domainWhiteList: [ '*' ],
  };
  config.cors = {
    origin: ctx => ctx.get('origin'), // 这种方式是允许所有的IP+端口跨域访问
    credentials: true, // 开启认证
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS', // 允许请求的方式
  };
  config.elasticsearch = {
    host: 'es:9200',
    apiVersion: '7.6',
  };
  const swaggerEgg = {
    schema: {
      path: '/app/schema', // JSON Schema directory
    },
    swagger: {
      info: {
        title: 'Test swagger 接口文档',
        description: '用来学习 swagger 的文档',
        version: '0.1.0',
      },
      externalDocs: {
        url: 'https://swagger.io',
        description: 'Find more info here',
      },
      host: '127.0.0.1:7001', // 应当与egg server host保持一致，否则会有跨域的问题
      schemes: [ 'http', 'https' ],
      consumes: [ 'application/json' ],
      produces: [ 'application/json' ],
      tags: [
        { name: 'test', description: 'test 标签的描述' },
      ],
      securityDefinitions: {
        api_key: {
          type: 'apiKey', // basic/apiKey/oauth2
          name: 'Authorization', // 自定义的鉴权参数，通常为'Authorization'
          in: 'header', // 鉴权参数放置的位置，query 或者 header
        },
        swagger_auth: {
          type: 'oauth2',
          authorizationUrl: 'http://swagger.io/api/oauth/dialog',
          flow: 'implicit',
          scopes: {
            'write:homes': 'modify home info',
            'read:homes': 'read home info',
          },
        },
      },
      security: [
        {
          api_key: [], // 鉴权方式（securityDefinitions中定义的内容）
        },
      ], // 注意: security为数组类型
    },
  };

  return {
    ...config,
    ...userConfig,
    swaggerEgg,
  };
};

